package com.jsyh.maidu.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.NearbyShopDetailActivity;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.model.NearbyShop;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/6/23.
 */

public class NearbyShopAdapter extends RecyclerView.Adapter<NearbyShopAdapter.NearbyHolder> {

    private List<NearbyShop> datas;
    private Context context;

    public NearbyShopAdapter(List<NearbyShop> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public NearbyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_nearby_shop, parent, false);
        NearbyHolder holder = new NearbyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(NearbyHolder holder, final int position) {
        holder.address.setText(datas.get(position).getAddress());
        holder.concact.setText(datas.get(position).getTel());
        holder.name.setText(datas.get(position).getShop_name());
        holder.distance.setText(datas.get(position).getDistance());
        Picasso.with(context).load(ConfigValue.IMG_IP + datas.get(position).getImg()).into(holder.img);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NearbyShopDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("shop", datas.get(position));
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class NearbyHolder extends RecyclerView.ViewHolder {

        ImageView img;
        TextView name;
        TextView concact;
        TextView address;
        TextView distance;
        TextView more;

        View view;


        public NearbyHolder(View itemView) {
            super(itemView);
            view = itemView;
            img = (ImageView) itemView.findViewById(R.id.nearby_shop_img);
            name = (TextView) itemView.findViewById(R.id.nearby_shop_name);
            concact = (TextView) itemView.findViewById(R.id.nearby_contact);
            address = (TextView) itemView.findViewById(R.id.nearby_address);
            distance = (TextView) itemView.findViewById(R.id.nearby_distance);
            more = (TextView) itemView.findViewById(R.id.nearby_more);
        }
    }
}
